$(document).ready(
		function() {
			$("#connexionInscription").children("button").each(
					function(idx, elem) {
						if (elem.getAttribute("value") == "connexion") {
							elem.setAttribute("onclick", "connexion()");
						}
						if (elem.getAttribute("value") == "inscription") {
							elem.setAttribute("onclick", "inscription()");
						}
					});

			$("#listeParties").children("tbody").each(
					function(idx, elem) {
						var trList = document.getElementsByTagName("tr");
						for ( var trs in trList) {
							if ((trList[trs].attributes != undefined)
									&& (trList[trs].attributes.length != 0))
								trList[trs].setAttribute("onclick",
										"rejoindrePartieSubmit('"
												+ trList[trs]
														.getAttribute("id")
												+ "')");
						}
					});

			$("#listeJournaux").children("tbody").each(
					function(idx, elem) {
						var trList = document.getElementsByTagName("tr");
						for ( var trs in trList) {
							if ((trList[trs].attributes != undefined)
									&& (trList[trs].attributes.length != 0)) {
								trList[trs].setAttribute("onclick",
										"afficherLog('"
												+ trList[trs]
														.getAttribute("id")
												+ "')");
							}
						}
					});

			$("[name='cochon']").click(function() {
				var cochonId = $(this).attr("id");

				var form = document.createElement("form");
				form.setAttribute("id", "joueCochon");
				form.setAttribute("cochonId", cochonId);
				form.setAttribute("method", "post");
				form.setAttribute("action", "jouer.html");

				var inputTxt = document.createElement("input");
				inputTxt.setAttribute("type", "hidden");
				inputTxt.setAttribute("type", "text");
				inputTxt.setAttribute("id", "cochonId");
				inputTxt.setAttribute("name", "cochonId");
				inputTxt.setAttribute("value", cochonId);
				form.appendChild(inputTxt);

				document.body.appendChild(form);
				document.getElementById("joueCochon").submit();
			});

		});

function connexion() {
	var inscriptElem = document.createElement("input");
	inscriptElem.setAttribute("id", "typeDAction");
	inscriptElem.setAttribute("name", "typeDAction");
	inscriptElem.setAttribute("type", "hidden");
	inscriptElem.setAttribute("value", "connexion");
	document.getElementById("connexionInscription").appendChild(inscriptElem);
	document.getElementById("connexionInscription").submit();
}

function inscription() {
	var inscriptElem = document.createElement("input");
	inscriptElem.setAttribute("id", "typeDAction");
	inscriptElem.setAttribute("name", "typeDAction");
	inscriptElem.setAttribute("type", "hidden");
	inscriptElem.setAttribute("value", "inscription");
	document.getElementById("connexionInscription").appendChild(inscriptElem);
	document.getElementById("connexionInscription").submit();
}

function rejoindrePartieSubmit(nomPartie) {
	var form = document.createElement("form");
	form.setAttribute("id", "rejoindrePartie");
	form.setAttribute("method", "post");
	form.setAttribute("action", "listedesparties.html");

	var inputTxt = document.createElement("input");
	inputTxt.setAttribute("type", "text");
	inputTxt.setAttribute("id", "nomPartie");
	inputTxt.setAttribute("name", "nomPartie");
	inputTxt.setAttribute("value", nomPartie);
	form.appendChild(inputTxt);

	document.body.appendChild(form);
	document.getElementById("rejoindrePartie").submit();
}

function xmlHttpPost(strUrl, data) {

	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open("POST", strUrl, true);
	self.xmlHttpReq.setRequestHeader('Content-Type',
			'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			document.getElementById("reponseChargement").innerHTML = self.xmlHttpReq.responseText;
		}
	};
	self.xmlHttpReq.send(data);

}

function afficherLog(nomPartie) {

	xmlHttpPost("listedesjournaux.html", "nomPartie=" + nomPartie + "");

	afficher_lightbox('1');

}
